<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/14
 * Time: 13:03
 */
namespace app\admin\controller;

use app\admin\validate\Setting as SettingModel;
use app\admin\model\Setting as model;

class Setting extends Base {

	protected $isSink = false;
	protected $sinkMethods = [];

    public function __construct(){
        parent::__construct();
    }

    /**
     * @param array $where
     * @param string $order
     * @param int $sql
     * @return array
     * @throws \think\exception\DbException
     */
    public function index($where=[],$order='id desc'){
        $column = new model;
        $_column =$column::where($where)->order($order)->find();
        if(!$_column){
            $this->__e('',[
                'status'=>0,
                'msg'=>'无数据'
            ]);
        }
        $this->assign('info',$_column);
        return view();
    }

    public function insert($id=0){
        $data = request()->post();
        $validate = new SettingModel;
        if(!$validate->check($data)){
            return $this->__e($validate->getError());
        }
        $column = new model;
        if($id){
            if(!$column->allowField(true)->isUpdate(true)->save($data)){
                return $this->__e('操作失败');
            }
            $info = $column::get($id);
            session('_site',$info->toArray());
            return $this->__s('操作成功');
        }else{
            if(!$column->allowField(true)->save($data)){
                return $this->__e('操作失败');
            }
            return $this->__s('操作成功');
        }
    }
}